package com.ruitu.health_1.ui.fragment.order;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.google.gson.Gson;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.dialog.CustomAlertDialog;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.OrderList;
import com.ruitu.health_1.dao.bean.Pay;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.ui.OrderDetailsActivity;
import com.ruitu.health_1.ui.PayOrderActivity;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.user.Address;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WaitingPayFragment extends NoCacheLoadingFragment<OrderPresenter.OrderP, OrderModel.OrderM> {

    public static WaitingPayFragment newInstance() {

        Bundle args = new Bundle();

        WaitingPayFragment fragment = new WaitingPayFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private StoreDataAdapter adapter;

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new StoreDataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        size = 10;
        getDataList();
    }

    private int size = 10;

    @Override
    protected void reqMore() {
        size = size + 10;
        getDataList();
    }

    //待支付订单请求
    private void getDataList() {
        p.orderList(1, size, AppHolder.instance().getUser().getId(), "0");
    }

    @Override
    public void requestSuc() {
        super.requestSuc();
        dialog.cancel();
        getDataList();
        Arad.bus.post(2020010800);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    private List<Integer> skipId;

    @Override
    protected void initViews(View v) {
        skipId = new ArrayList<>();
        showLoadingView(skipId);
        showProgress();
        getDataList();
    }

    @Override
    protected ProgressLayout findProgressLayout(View v) {
        return v.findViewById(R.id.progress);
    }

    @Override
    protected LRecyclerView findRecyclerView(View v) {
        return v.findViewById(R.id.l_recyclerview);
    }

    private List<StoreOrderItemList> orderLists;

    @Override
    public void setData(List<StoreOrderItemList> orderLists) {
        this.orderLists = orderLists;
        showContent(skipId);
        Logg.i("Order:  " + orderLists.toString());
        adapter.setDataList(orderLists);
    }

    @Override
    public void setWaitPayData(Pay payData) {
        super.setWaitPayData(payData);
        Bundle bundle = new Bundle();
        Address address = new Address();
        address.setName(payData.getReceiverName());
        address.setPhoneNumber(payData.getReceiverPhone());
        address.setProvince(payData.getReceiverProvince());
        address.setCity(payData.getReceiverCity());
        address.setDetailAddress(payData.getReceiverDetailAddress());
        bundle.putSerializable("address", address);
        bundle.putSerializable("pay", payData);
        startActivity(PayOrderActivity.class, bundle);
    }

    private CustomAlertDialog dialog;

    class DataAdapter extends ListBaseAdapter<OrderList> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_order_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            final OrderList orderList = mDataList.get(position);
            LinearLayout ll_item = holder.getView(R.id.ll_item);
            RecyclerView recyclerview = holder.getView(R.id.recyclerview);
            View view = holder.getView(R.id.divider);

            view.setVisibility(position > 0 ? View.VISIBLE : View.GONE);

            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            ArrayList<StoreOrderItemList> storeOrderItemList = orderList.getStoreOrderItemList();

            recyclerview.setLayoutManager(manager);
            StoreDataAdapter storeDataAdapter = new StoreDataAdapter(mContext);
            recyclerview.setAdapter(storeDataAdapter);
            storeDataAdapter.setDataList(storeOrderItemList);

        }
    }

    private List list;

    public void goToPay() {

    }

    class StoreDataAdapter extends ListBaseAdapter<StoreOrderItemList> {
        public StoreDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_store_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RecyclerView recyclerview = holder.getView(R.id.l_recyclerview);
            TextView tv_store_name = holder.getView(R.id.tv_store_name);
            TextView tv_go_pay = holder.getView(R.id.tv_go_pay);
            TextView tv_cancel = holder.getView(R.id.tv_cancel);
            TextView tv_pay_detail = holder.getView(R.id.tv_pay_detail);
            LinearLayout ll_wait_pay = holder.getView(R.id.ll_wait_pay);

            ll_wait_pay.setVisibility(View.VISIBLE);
            final StoreOrderItemList storeOrderItemList = mDataList.get(position);
            final List<OrderItemList> orderItemList = storeOrderItemList.getOrderItemList();
            tv_store_name.setText(storeOrderItemList.getName());
            ChildDataAdapter childDataAdapter = new ChildDataAdapter(mContext);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);

            recyclerview.setLayoutManager(manager);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(orderItemList);


            tv_go_pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Pay pay = new Pay();
//                    pay.setOrderId(ids(orderItemList));
//                    pay.setPayAmount(storeOrderItemList.getPayAmount());
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("address", storeOrderItemList.getReceiveAddress());
//                    bundle.putSerializable("pay", pay);
//                    startActivity(PayOrderActivity.class, bundle);
                    list = new ArrayList();
                    for (int i = 0; i < orderItemList.size(); i++) {
                        list.add(orderItemList.get(i).getId());
                    }
                    Map map = new HashMap();
                    map.put("ids", new Gson().toJson(list));
                    showProgress();
                    p.payOldorder(ReqUtil.getBody(map, true));
                }
            });

            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog = new CustomAlertDialog(mContext, "确定取消当前订单么?", new CustomAlertDialog.OkOnClickListener() {
                        @Override
                        public void OkOnClick() {
                            Map<String, String> map = new HashMap<>();
                            map.put("ids", ids(orderItemList));
                            map.put("memberId", AppHolder.instance().getUser().getId());
                            p.cancelOrder(ReqUtil.getBody(map, true));

                        }
                    });
                    dialog.show();
                }
            });

            tv_pay_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("orderId", ids(orderItemList));
                    startActivity(OrderDetailsActivity.class, bundle);
                }
            });
        }
    }

    class ChildDataAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_child_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);

            OrderItemList listBean = mDataList.get(position);
            Glide.with(mContext).load(listBean.getProductPic()).apply(Arad.opts).into(iv_commodity);

            tv_commodity_name.setText(listBean.getProductName());
            GoodsInfo good = listBean.getGood();
            StringBuffer buffer = new StringBuffer();
            if (good != null) {
                tv_commodity_num.setText(good.getProductQuantity());
                tv_commodity_price.setText(good.getProductPrice());
                if (!TextUtils.isEmpty(good.getSp1())) {
                    buffer.append(good.getSp1());
                }
                if (!TextUtils.isEmpty(good.getSp2())) {
                    buffer.append("," + good.getSp2());
                }
                if (!TextUtils.isEmpty(good.getSp3())) {
                    buffer.append("," + good.getSp3());
                }
            }

            tv_commodity_info.setText(buffer.toString());

        }
    }

    private String ids(List<OrderItemList> orderItemList) {
        list = new ArrayList();
        for (int i = 0; i < orderItemList.size(); i++) {
            list.add(orderItemList.get(i).getId());
        }
        return new Gson().toJson(list);
    }
}
